package org.zlui65.milan.examples

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.StyleRes
import org.zlui65.milan.Style
import org.zlui65.milan.StyleBuilder
import org.zlui65.milan.annotations.Attr
import org.zlui65.milan.annotations.Style
import org.zlui65.milan.annotations.Styleable
import org.zlui65.milan.annotations.StyleableChild
import org.zlui65.milan.backgroundColor
import org.zlui65.milan.bold
import org.zlui65.milan.padding
import org.zlui65.milan.style
import org.zlui65.milan.textColor
import org.zlui65.milan.textSize

/**
 * Example of a custom view that uses Milan annotations.
 */
@Styleable("MyCustomView")
class MyCustomView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr) {

    @StyleableChild(R.styleable.MyCustomView_titleStyle)
    internal val titleTextView: TextView = TextView(context)

    @StyleableChild(R.styleable.MyCustomView_subtitleStyle)
    internal val subtitleTextView: TextView = TextView(context)

    private var title: String = ""
    private var subtitle: String = ""

    init {
        // Add the text views to the layout
        addView(titleTextView)
        addView(subtitleTextView)

        // Apply the attribute set if provided
        if (attrs != null) {
            style(attrs)
        }
    }

    @Attr(R.styleable.MyCustomView_title)
    fun setTitle(title: String) {
        this.title = title
        titleTextView.text = title
    }

    @Attr(R.styleable.MyCustomView_subtitle)
    fun setSubtitle(subtitle: String) {
        this.subtitle = subtitle
        subtitleTextView.text = subtitle
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        // Simple vertical layout
        val width = r - l
        var currentTop = 0

        // Layout title
        val titleHeight = titleTextView.measuredHeight
        titleTextView.layout(0, currentTop, width, currentTop + titleHeight)
        currentTop += titleHeight

        // Layout subtitle
        val subtitleHeight = subtitleTextView.measuredHeight
        subtitleTextView.layout(0, currentTop, width, currentTop + subtitleHeight)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        // Measure children
        measureChild(titleTextView, widthMeasureSpec, heightMeasureSpec)
        measureChild(subtitleTextView, widthMeasureSpec, heightMeasureSpec)

        // Calculate total height
        val totalHeight = titleTextView.measuredHeight + subtitleTextView.measuredHeight
        val width = MeasureSpec.getSize(widthMeasureSpec)

        // Set measured dimensions
        setMeasuredDimension(width, totalHeight)
    }

    companion object {
        // Example of linking styles to the view
        @Style
        val DARK_STYLE = R.style.MyCustomView_Dark

        // Example of a programmatically defined style
        @Style
        val LIGHT_STYLE = org.zlui65.milan.Style.create().apply {
            // Apply styles to the view itself
            setAttribute(android.R.attr.background, Color.WHITE)
            setAttribute(android.R.attr.padding, 16)

            // Apply styles to child views
            setAttribute(R.styleable.MyCustomView_titleStyle, R.style.Title_Light)
            setAttribute(R.styleable.MyCustomView_subtitleStyle, R.style.Subtitle_Light)
        }
    }
}

/**
 * Example of how to use the Milan library in an activity or fragment.
 */
class ExampleUsage {
    fun styleViews(context: Context) {
        // Create a custom view
        val myCustomView = MyCustomView(context)

        // Apply an XML-defined style
        myCustomView.style(R.style.MyCustomView_Dark)

        // Apply a programmatically defined style
        myCustomView.style {
            // Add a predefined style
            add(R.style.MyCustomView_Light)

            // Customize specific attributes
            padding(24)
        }

        // Combine multiple styles
        myCustomView.style {
            add(R.style.MyCustomView_Dark)
            add(R.style.MyCustomView_Light)
            // Last style added takes precedence for overlapping attributes
        }

        // Style a regular TextView
        val textView = TextView(context)
        textView.style {
            textColor(Color.GREEN)
            textSize(18f)
            bold()
            backgroundColor(Color.BLACK)
        }

        // Style child views
        myCustomView.style {
            // Style the title using a resource
            titleStyle(R.style.Title_Dark)

            // Style the subtitle programmatically
            subtitleStyle {
                textColor(Color.GRAY)
                textSize(14f)
            }
        }
    }
}
